<template>
  <div :class="['ec-alert-' + position, show ? 'animate-show' : '']" class="ec-alert-fixed" v-if="showIn">
    <div class="ec-alert" :style="{ background: background, color: color }">
      <div class="ec-alert-icon" v-if="iconClass" :class="iconClass"></div>
      <div class="ec-alert-content">{{content}}</div>
    </div>
  </div>
</template>
<script>
  let sleep = 0
  let sleepAn = 0
  export default {
    name: 'ec-toast',
    data () {
      return {
        show: false,
        showIn: false,
        content: '',
        background: '',
        color: '',
        iconClass: '',
        position: 'center'
      }
    },
    methods: {
      close () {
        this.showIn = false
        this.show = false
        this.content = ''
        this.background = ''
      },
      open ({ title = '', position = 'center', duration = 2600, iconClass = 'ec-icons icon-information-outline', background = 'rgba(0,0,0,0.6)', color = '#ddd' }) {
        this.show = false
        clearTimeout(sleep)
        clearTimeout(sleepAn)
        sleep = setTimeout(() => {
          this.content = title
          this.background = background
          this.color = color
          this.iconClass = iconClass
          this.position = position
          this.show = true
          this.showIn = true
          sleepAn = setTimeout(() => {
            this.close()
          }, duration)
          sleep = setTimeout(() => {
            this.close()
          }, duration * 2)
        }, 200)
      }
    }
  }
</script>
<style scoped lang="scss">
  @keyframes g-show1 {
    0% {
      transform: translate(0, -50%) scale(0, 0);
      opacity: 0;
    }

    33% {
      transform: translate(0, -50%) scale(1, 1);
      opacity: 1;
    }

    66% {
      transform: translate(0, -50%) scale(1.24, 1.24);
      opacity: 1;
    }

    100% {
      transform: translate(0, -50%) scale(1, 1);
      opacity: 1;
    }
  }

  @keyframes g-show2 {
    0% {
      transform: scale(0, 0);
      opacity: 0;
    }

    33% {
      transform: scale(1, 1);
      opacity: 1;
    }

    66% {
      transform: scale(1.24, 1.24);
      opacity: 1;
    }

    100% {
      transform: scale(1, 1);
      opacity: 1;
    }
  }

  .ec-alert-fixed {
    position: fixed !important;
    left: 0;
    z-index: 99999;
    width: 100%;
    text-align: center;
  }

  .ec-alert {
    position: relative;
    display: inline-block;
    max-width: 80vw;
    padding: 15px 30px;
    font-size: 15px;
    background: #fff;
    border-radius: 4px;
  }

  .ec-alert::before {
    position: absolute;
    top: 2px;
    left: 2px;
    z-index: -1;
    display: block;
    width: 100%;
    height: 100%;
    background: inherit;
    border-radius: inherit;
    transform: scale(1, 1);
    transform-origin: 0 0;
    opacity: 0.2;
    filter: blur(3px);
    content: "";
  }
  .ec-alert-icon { padding-bottom: 3px; font-size: 21px; text-align: center; }

  .ec-alert-top {
    top: 8px;
    bottom: auto;

    /* #ifdef H5 */
    margin-top: var(--window-top);

    /* #endif */
    text-align: center;
  }
  .ec-alert-center { top: 45%; transform: translate(0, -50%); }
  .ec-alert-bottom { top: auto; bottom: 8px; }
  .animate-show.ec-alert-center { animation: g-show1 360ms linear; }

  .ec-alert-top,
  .ec-alert-bottom {
    display: flex;
    justify-content: center;

    .ec-alert {
      display: flex;
      flex-wrap: nowrap;
      justify-content: center;
      padding-top: 13px;
      padding-bottom: 13px;
      text-align: left;
    }

    &.animate-show {
      animation: g-show2 360ms linear;
    }

    .ec-alert-icon {
      margin-right: 10px;
    }
  }
</style>
